using System;
using System.Collections.Generic;
using System.Xml;

namespace Mehat.San.SanFS.Cache
{
	public class CacheFactory
	{
		private static Dictionary<string, ICachePolicy>	mPolicies = new Dictionary<string, ICachePolicy>();
		private static ICachePolicy						mActivePolicy = null;
		
		public static void Initialize(XmlNode CfgNode)
		{
			/*
			 * Add all built-in policies
			 */
			 AddPolicy(new DummyCachePolicy());
			
			/*
			 * Configure policy based on XML configuration
			 */
			 XmlNode	strategyNode = CfgNode.Attributes.GetNamedItem("Strategy");
			
			if (!mPolicies.ContainsKey(strategyNode.Value))
				throw new Exception(String.Format("Strategy '{0}' does not exist", strategyNode.Value));
			mActivePolicy = mPolicies[strategyNode.Value].CreateInstance(CfgNode);
		}
		
		private static void AddPolicy(ICachePolicy Policy)
		{
			mPolicies.Add(Policy.PolicyName, Policy);
		}
		
		public static ICachePolicy ActivePolicy
		{
			get { return mActivePolicy; }
		}
	}  // END CLASS
}  // END NAMESPACE
